<?php
/**
 * Created by PhpStorm.
 * User: xh
 * Date: 2021/8/6
 * Time: 10:28
 */

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";


class CON
{

    private $servername = "localhost";
    private $username = "root";
    private $password = 'xianghai';
    private $dbname = 'changge';
    private $conn;

    public function __construct()
    {

        $this->conn = new PDO("mysql:host=" . $this->servername . ";dbname=" . $this->dbname, $this->username, $this->password);


    }

    /**
     * 新增
     */
    public function add($sql)
    {

        try {
            $this->conn -> exec($sql);
            return $this->conn->lastInsertId();
        } catch (PDOException $e) {
            return false;
        }


    }

    /**
     * 查找
     */
    public function query($sql)
    {

        try {
            $pdoStatement = $this->conn->query($sql, PDO::FETCH_ASSOC);
            $data = $pdoStatement->fetchAll();
            return $data;

        } catch (PDOException $e) {
            return false;
        }


    }


}